package main

import (
	"encoding/hex"
	"flag"
	"fmt"

	"gitee.com/thubcc/p2pgit/gitfs/storage"
	"gitee.com/thubcc/p2pgit/some"
	"gitee.com/thubcc/p2pgit/types"
	"gopkg.in/src-d/go-git.v4/plumbing"
)

var (
	argNewPack = flag.String("n", "../temp", "New Pack Dir")
	argURL     = flag.String("u", "http://localhost:8080", "server url")
	argHash    = flag.String("h", "26a54b59539b31b6e5e897c30b7664e2e9db9cbb", "object hash")
)

func main() {
	flag.Parse()

	h, err := hex.DecodeString(*argHash)
	if err != nil {
		fmt.Println("Bad Hash", *argHash, err)
		return
	}
	var hash types.Hash
	copy(hash[:], h)
	gs, err := some.GeterFromDir(*argNewPack)
	if err != nil {
		fmt.Println("find pack failure,", *argNewPack, err)
		return
	}
	rs := types.NewRawer(some.Geter2Rawer(gs))
	rs.Open(false)
	defer rs.Close()
	body, t, err := types.ResolveObj(gs, types.HashSelect(hash))
	if err != nil {
		fmt.Println("ResolveObj failure,", err)
		return
	}
	fmt.Println("cat", hash.String())
	if t == plumbing.TreeObject {
		s := storage.NewStorage(gs)
		tree, err := storage.GetTree(s, hash)
		if err != nil {
			fmt.Println("get tree failure,", err)
			return
		}
		fmt.Println(tree.String())
	} else {
		fmt.Println(t.String())
		fmt.Println(string(body))
	}
}
